package br.com.dev.tcc.view.corretor;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import br.com.dev.tcc.R;
import br.com.dev.tcc.model.ListaCorretoresModel;
import br.com.dev.tcc.view.DefaultSupportActionBarActivity;
import br.com.dev.tcc.view.dialog.SimpleListDialog;
import br.com.dev.tcc.view.util.BuildPhoneItensPopup;

public class ListaCorretoresActivity extends DefaultSupportActionBarActivity {

	private ListaCorretoresModel model;
	private ListaCorretoresView view;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.corretores_lista);
		setActionBar(R.string.corretor, R.drawable.corretor);

		model = new ListaCorretoresModel(this);
		view = new ListaCorretoresView(this);

		setList();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		if (resultCode == RESULT_OK) {

			model = new ListaCorretoresModel(this);
			setList();
		}
	}

	private void setList() {

		if (model.hasItens()) {

			view.setItens(model.getListCorretores());
			view.setClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> a, View v, int pos,
						long id) {

					View ivCall = v.findViewById(R.id.ivCall);
					if (ivCall != null)
						ivCall.startAnimation(AnimationUtils.loadAnimation(
								ListaCorretoresActivity.this,
								R.anim.scale_accelerate));

					SimpleListDialog dialog = new SimpleListDialog(
							ListaCorretoresActivity.this, BuildPhoneItensPopup
									.buildPhoneItens(model.getCorretor(pos)));
					dialog.show();
				}
			});

			view.setLongClickListener(new OnItemLongClickListener() {

				@Override
				public boolean onItemLongClick(AdapterView<?> a, View view,
						int pos, long id) {

					SimpleListDialog dialog = new SimpleListDialog(
							ListaCorretoresActivity.this, BuildPhoneItensPopup
									.buildPhoneItens(model.getCorretor(pos)));
					dialog.show();
					return true;
				}
			});
		} else {

			view.hideList();
		}
	}
}
